home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wtj208.zip / FRANTZ / TARGET / TARGET.C < prev    next >
C/C++ Source or Header  |  1993-04-10  |  11KB  |  460 lines

  1. /* ------------------------------ TARGET.C ---------------------------
  2.  *    Target control Window Procedure
  3.  * ---------------------------------------------------------------- */
  4.  
  5. #include "Windows.h"
  6. #include "Target_.h"
  7.  
  8. // ---- Extra bytes indexes
  9. #define XCUR        0
  10. #define YCUR        2
  11. #define FCAPTURE    4
  12. #define VBBMP        6                // Bitmap handle (for VB)
  13.  
  14. /* -------------------------- DrawOurFrame ----------------------------
  15.  *    Draws the frame
  16.  * ---------------------------------------------------------------- */
  17.  
  18. static VOID DrawOurFrame (HWND hWnd, HDC hDC, BOOL fOn)
  19. {
  20. RECT            Rect;
  21. HBRUSH            hBr;
  22.  
  23.     GetClientRect (hWnd, &Rect);
  24.  
  25.     if (fOn)
  26.         FrameRect (hDC, &Rect, GetStockObject (GRAY_BRUSH));
  27.     else
  28.     {
  29.         hBr = CreateSolidBrush (
  30.             GetSysColor (GetWindowLong (hWnd, GWL_STYLE) & TGS_BORDER?
  31.                                     COLOR_WINDOWFRAME: COLOR_WINDOW));
  32.         FrameRect (hDC, &Rect, hBr);
  33.         DeleteObject (hBr);
  34.     }
  35. }
  36.  
  37. /* -------------------------- DrawCross -------------------------------
  38.  *    Draws the pointer
  39.  * ---------------------------------------------------------------- */
  40.  
  41. static VOID DrawCross (HWND hWnd)
  42. {
  43. HDC                hDC;
  44. HPEN            hPen, hPenOld;
  45. int                xCur,
  46.                 yCur;                // Current position
  47.  
  48.     xCur = GetWindowWord (hWnd, XCUR);
  49.     yCur = GetWindowWord (hWnd, YCUR);
  50.  
  51.     hDC = GetDC (hWnd);
  52.     SetROP2 (hDC, R2_NOT);
  53.     hPen = CreatePen (PS_SOLID, 3, RGB (0, 0, 0));
  54.     hPenOld = SelectObject (hDC, hPen);
  55.  
  56.     MoveTo (hDC, xCur-10, yCur-10);
  57.     LineTo (hDC, xCur+10, yCur+10);
  58.     MoveTo (hDC, xCur-10, yCur+10);
  59.     LineTo (hDC, xCur+10, yCur-10);
  60.  
  61.     SelectObject (hDC, hPenOld);
  62.     DeleteObject (hPen);
  63.     ReleaseDC (hWnd, hDC);
  64.  
  65.     SetWindowWord (hWnd, XCUR, xCur);
  66.     SetWindowWord (hWnd, YCUR, yCur);
  67.     return;
  68. }
  69.  
  70. /* -------------------------- wmKeyboard ----------------------------
  71.  *    Processes WM_CHAR, WM_KEYDOWN et WM_KEYUP messages
  72.  *        wMsg is the message
  73.  *        wParam is the caracter (WM_CHAR) or virtual key (others)
  74.  *        lParam : cf doc
  75.  * ---------------------------------------------------------------- */
  76.  
  77. static VOID wmKeyboard (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam)
  78. {
  79. static int        nInc;                // Move increment
  80. RECT            Rect;
  81. int                r;
  82. int                xCur,
  83.                 yCur;                // Current position
  84.  
  85.     if (wMsg == WM_KEYDOWN &&
  86.         (wParam == VK_DOWN ||
  87.          wParam == VK_UP ||
  88.          wParam == VK_RIGHT ||
  89.          wParam == VK_LEFT))
  90.     {
  91.         DrawCross (hWnd);            // Erases cross
  92.  
  93.         if (lParam & (1L<<30))        // Key was down
  94.             nInc++;
  95.         else
  96.             nInc = 1;
  97.  
  98.         r = LOWORD (lParam);        // Repeat count, is 1 or more
  99.         if (r)
  100.             while (--r)
  101.                 nInc++;
  102.  
  103.         xCur = GetWindowWord (hWnd, XCUR);
  104.         yCur = GetWindowWord (hWnd, YCUR);
  105.  
  106.         switch (wParam)
  107.         {
  108.         case VK_DOWN:
  109.             yCur += nInc;
  110.             break;
  111.  
  112.         case VK_UP:
  113.             yCur -= nInc;
  114.             break;
  115.  
  116.         case VK_RIGHT:
  117.             xCur += nInc;
  118.             break;
  119.  
  120.         case VK_LEFT:
  121.             xCur -= nInc;
  122.             break;
  123.         }
  124.  
  125.         GetClientRect (hWnd, &Rect);    // Test max/min
  126.         yCur = max (yCur, 0);
  127.         yCur = min (yCur, Rect.bottom);
  128.         xCur = max (xCur, 0);
  129.         xCur = min (xCur, Rect.right);
  130.         SetWindowWord (hWnd, XCUR, xCur);
  131.         SetWindowWord (hWnd, YCUR, yCur);
  132.  
  133.         DrawCross (hWnd);
  134.  
  135. // ---- Sends notification
  136.         SendMessage (GetParent (hWnd), WM_COMMAND,
  137.                     GetWindowWord (hWnd, GWW_ID), MAKELONG (hWnd, TGN_CHANGE));
  138.     }
  139.  
  140.     return;
  141. }
  142.  
  143. /* -------------------------- wmCreate ------------------------------
  144.  *    Processes WM_CREATE message
  145.  *        lpCrst (lParam) points to a CREATESTRUCT structure
  146.  * ---------------------------------------------------------------- */
  147.  
  148. static VOID wmCreate (HWND hWnd, LPCREATESTRUCT lpCrst)
  149. {
  150.     SetWindowWord (hWnd, XCUR, lpCrst->cx/2);
  151.     SetWindowWord (hWnd, YCUR, lpCrst->cy/2);
  152.     SetWindowWord (hWnd, FCAPTURE, FALSE);
  153.  
  154. // ---- Sends notification
  155.     SendMessage (GetParent (hWnd), WM_COMMAND,
  156.                     GetWindowWord (hWnd, GWW_ID), MAKELONG (hWnd, TGN_CHANGE));
  157.     return;
  158. }
  159.  
  160. /* -------------------------- wmMouse -------------------------------
  161.  *    Processes WM_MOUSEMOVE and WM_?BUTTON* messages
  162.  *        wMsg is the message
  163.  *        wParam indicates keys or buttons pressed
  164.  *        x (LO lParam) is x coordinate of pointer
  165.  *        y (HI lParam) is y coordinate of pointer
  166.  * ---------------------------------------------------------------- */
  167.  
  168. static VOID wmMouse (HWND hWnd, WORD wMsg, WORD wParam, int x, int y)
  169. {
  170. RECT            Rect;
  171.  
  172. // ---- Keeps cros within rectangle
  173.  
  174.     switch (wMsg)
  175.     {
  176.     case WM_LBUTTONDOWN:
  177.         SetFocus (hWnd);
  178.  
  179.         SetCapture (hWnd);
  180.         SetWindowWord (hWnd, FCAPTURE, TRUE);
  181.         ShowCursor (FALSE);
  182.  
  183.         DrawCross (hWnd);
  184.         SetWindowWord (hWnd, XCUR, x);
  185.         SetWindowWord (hWnd, YCUR, y);
  186.         DrawCross (hWnd);
  187.  
  188.         GetClientRect (hWnd, &Rect);
  189.         ClientToScreen (hWnd, (LPPOINT)&Rect);
  190.         ClientToScreen (hWnd, (LPPOINT)&Rect.right);
  191.         ClipCursor (&Rect);            // Clips cursor
  192.  
  193.         break;
  194.  
  195.     case WM_MOUSEMOVE:
  196.         if (GetWindowWord (hWnd, FCAPTURE))
  197.         {
  198.             DrawCross (hWnd);
  199.             SetWindowWord (hWnd, XCUR, x);
  200.             SetWindowWord (hWnd, YCUR, y);
  201.             DrawCross (hWnd);
  202.         }
  203.  
  204.         break;
  205.  
  206.     case WM_LBUTTONUP:
  207.         if (GetWindowWord (hWnd, FCAPTURE))
  208.         {
  209.             ClipCursor (NULL);        // Frees cursor
  210.             ReleaseCapture ();
  211.             SetWindowWord (hWnd, FCAPTURE, FALSE);
  212.             ShowCursor (TRUE);
  213.  
  214. // ---- Sends notification
  215.             SendMessage (GetParent (hWnd), WM_COMMAND,
  216.                     GetWindowWord (hWnd, GWW_ID), MAKELONG (hWnd, TGN_CHANGE));
  217.         }
  218.         break;
  219.     }
  220.     return;
  221. }
  222.  
  223. /* -------------------------- wmPaint -------------------------------
  224.  *    Processes WM_PAINT message
  225.  * ---------------------------------------------------------------- */
  226.  
  227. static VOID wmPaint (HWND hWnd)
  228. {
  229. PAINTSTRUCT        ps;
  230. HDC                hDC;
  231. RECT            Rect;
  232. HBRUSH            hBr, hBrOld;
  233. int                X, Y;
  234. register        i;
  235. char            szText[256];
  236. HBITMAP            hBmp, hBmpOld;
  237. HDC                hDCMem;
  238. BITMAP            Bmp;
  239. static COLORREF    Color [] =
  240. {
  241.     0x00000000L, 0x000000ffL, 0x0000ff00L, 0x0000ffffL,
  242.     0x00ff0000L, 0x00ff00ffL, 0x00ffff00L, 0x00ffffffL
  243. };
  244.  
  245. // ---- Hides cross
  246.     DrawCross (hWnd);
  247.  
  248.     hDC = BeginPaint (hWnd, &ps);
  249.  
  250. // ---- Draws background
  251.     GetClientRect (hWnd, &Rect);
  252.     switch (GetWindowLong (hWnd, GWL_STYLE) & 7)    // 3 lower bits
  253.     {
  254.     case TGS_CIRCLES:
  255.         InflateRect (&Rect, -1, -1);
  256.         X = -Rect.right/16;
  257.         Y = -Rect.bottom/16;
  258.         for (i = 0; i < 8; ++i)
  259.         {                            // Draws 5 ellipses
  260.             hBr = CreateSolidBrush (Color[i]);
  261.             hBrOld = SelectObject (hDC, hBr);
  262.             Ellipse (hDC, Rect.left, Rect.top, Rect.right, Rect.bottom);
  263.             InflateRect (&Rect, X, Y);
  264.             SelectObject (hDC, hBrOld);
  265.             DeleteObject (hBr);
  266.         }
  267.         break;
  268.  
  269.     case TGS_PLAIN:
  270.         hBr = (HBRUSH)SendMessage (GetParent (hWnd), WM_CTLCOLOR,
  271.                                     hDC, MAKELONG (hWnd, 0));
  272.         if (hBr)
  273.             FillRect (hDC, &Rect, hBr);
  274.         break;
  275.  
  276.     case TGS_BITMAP:
  277.         if (GetWindowLong (hWnd, GWL_STYLE) & TGS_VB)
  278.             hBmp = GetWindowWord (hWnd, VBBMP);
  279.         else
  280.         {
  281.             GetWindowText (hWnd, szText, sizeof szText);
  282.             hBmp = LoadBitmap (GetWindowWord (hWnd, GWW_HINSTANCE), szText);
  283.         }
  284.  
  285.         if (hBmp)
  286.         {
  287.             hDCMem = CreateCompatibleDC (hDC);
  288.             hBmpOld = SelectObject (hDCMem, hBmp);
  289.             GetObject (hBmp, sizeof (BITMAP), (LPSTR)&Bmp);
  290. //            BitBlt (hDC, 0, 0, Bmp.bmWidth, Bmp.bmHeight, hDCMem, 0, 0, SRCCOPY);
  291.             StretchBlt (hDC, 0, 0, Rect.right, Rect.bottom, hDCMem, 0, 0, Bmp.bmWidth, Bmp.bmHeight, SRCCOPY);
  292.             SelectObject (hDCMem, hBmpOld);
  293.             DeleteDC (hDCMem);
  294.         }
  295.         break;
  296.     }
  297.  
  298. // ---- Draws frame
  299.     DrawOurFrame (hWnd, hDC, GetFocus () == hWnd);
  300.  
  301.     EndPaint (hWnd, &ps);
  302.  
  303. // ---- Restores cross
  304.     DrawCross (hWnd);
  305.  
  306.     return;
  307. }
  308.  
  309. /* -------------------------- wmSize --------------------------------
  310.  *    Processes WM_SIZE message
  311.  *        wParam is type of size change
  312.  *        cx (LO lParam) is new width
  313.  *        cy (HI lParam) is new height
  314.  * ---------------------------------------------------------------- */
  315.  
  316. static VOID wmSize (HWND hWnd, WORD wParam, int cx, int cy)
  317. {
  318. // ---- Keeps cross within window
  319.     if (wParam != SIZEICONIC)
  320.     {
  321.         if ((int)GetWindowWord (hWnd, XCUR) > cx)
  322.             SetWindowWord (hWnd, XCUR, cx);
  323.         if ((int)GetWindowWord (hWnd, YCUR) > cy)
  324.             SetWindowWord (hWnd, YCUR, cy);
  325.     }
  326.  
  327.     return;
  328. }
  329.  
  330. /* -------------------------- wmSetFocus ----------------------------
  331.  *    Processes WM_SETFOCUS message
  332.  *        hWndOther (wParam) is window loosing focus
  333.  * ---------------------------------------------------------------- */
  334.  
  335. static VOID wmSetFocus (HWND hWnd, HWND hWndOther)
  336. {
  337. HDC                hDC;
  338.  
  339. // ---- Draws a gray frame
  340.     hDC = GetDC (hWnd);
  341.     DrawOurFrame (hWnd, hDC, TRUE);
  342.     ReleaseDC (hWnd, hDC);
  343.     return;
  344. }
  345.  
  346. /* -------------------------- wmKillFocus ---------------------------
  347.  *    Processes WM_KILLFOCUS message
  348.  *        hWndOther (wParam) is window getting focus
  349.  * ---------------------------------------------------------------- */
  350.  
  351. static VOID wmKillFocus (HWND hWnd, HWND hWndOther)
  352. {
  353. HDC                hDC;
  354.  
  355. // ---- Draws a normal frame
  356.     hDC = GetDC (hWnd);
  357.     DrawOurFrame (hWnd, hDC, FALSE);
  358.     ReleaseDC (hWnd, hDC);
  359.     return;
  360. }
  361.  
  362. /* -------------------------- tgmSetPos -----------------------------
  363.  *    Processes TGM_SETPOS message
  364.  *        x (LO lParam) is x pos
  365.  *        y (HI lParam) is y pos
  366.  * ---------------------------------------------------------------- */
  367.  
  368. static VOID tgmSetPos (HWND hWnd, int x, int y)
  369. {
  370.     DrawCross (hWnd);                // Erases cross
  371.     SetWindowWord (hWnd, XCUR, x);
  372.     SetWindowWord (hWnd, YCUR, y);
  373.     DrawCross (hWnd);                // Draws cross
  374.  
  375. // ---- Sends notification
  376.     SendMessage (GetParent (hWnd), WM_COMMAND,
  377.                     GetWindowWord (hWnd, GWW_ID), MAKELONG (hWnd, TGN_CHANGE));
  378. }
  379.  
  380. /* -------------------------- TargetWndProc ---------------------------
  381.  *    Target WndProc - Processes messages
  382.  * ---------------------------------------------------------------- */
  383.  
  384. LRESULT CALLBACK _export TargetWndProc (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam)
  385. {
  386. long            lRet = 0;            // Return value
  387. BOOL            fProcessed = TRUE;    // Indicates command processed
  388.  
  389.     switch (wMsg)
  390.     {
  391. // ---- Specific TARGET messages
  392.     case TGM_SETPOS:
  393.         tgmSetPos (hWnd, LOWORD (lParam), HIWORD (lParam));
  394.         break;
  395.  
  396.     case TGM_GETPOS:
  397.         lRet = MAKELONG (GetWindowWord (hWnd, XCUR),
  398.                          GetWindowWord (hWnd, YCUR));
  399.         break;
  400.  
  401.     case TGM_SETBMP:
  402.         SetWindowWord (hWnd, VBBMP, wParam);
  403.         break;
  404.  
  405. // ---- Windows messages
  406.     case WM_GETDLGCODE:
  407.         lRet = DLGC_WANTARROWS;        // Want arrows
  408.         break;
  409.  
  410.     case WM_CREATE:
  411.         wmCreate (hWnd, (LPCREATESTRUCT)lParam);
  412.         break;
  413.  
  414.     case WM_SETFOCUS:
  415.         wmSetFocus (hWnd, wParam);
  416.         break;
  417.  
  418.     case WM_KILLFOCUS:
  419.         wmKillFocus (hWnd, wParam);
  420.         break;
  421.  
  422.     case WM_PAINT:
  423.         wmPaint (hWnd);
  424.         break;
  425.  
  426.     case WM_SIZE:
  427.         wmSize (hWnd, wParam, LOWORD (lParam), HIWORD (lParam));
  428.         break;
  429.  
  430.     case WM_CHAR:
  431.     case WM_KEYDOWN:
  432.     case WM_KEYUP:
  433.         wmKeyboard (hWnd, wMsg, wParam, lParam);
  434.         break;
  435.  
  436.     case WM_MOUSEMOVE:
  437.     case WM_LBUTTONDOWN:
  438.     case WM_LBUTTONUP:
  439.     case WM_LBUTTONDBLCLK:
  440.     case WM_RBUTTONDOWN:
  441.     case WM_RBUTTONUP:
  442.     case WM_RBUTTONDBLCLK:
  443.     case WM_MBUTTONDOWN:
  444.     case WM_MBUTTONUP:
  445.     case WM_MBUTTONDBLCLK:
  446.         wmMouse (hWnd, wMsg, wParam, LOWORD (lParam), HIWORD (lParam));
  447.         break;
  448.  
  449.     default:
  450.         fProcessed = FALSE;
  451.         break;
  452.     }
  453.  
  454. // ---- Default processing
  455.     if (!fProcessed)
  456.         lRet = DefWindowProc (hWnd, wMsg, wParam, lParam);
  457.  
  458.     return lRet;
  459. }
  460.